﻿using System.IdentityModel.Tokens.Jwt;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using sixgod.Common.Model;

namespace sixgod.Base.Controllers
{
    [Authorize]
    [Route("[Controller]"),ApiController]
    public class BaseController : ControllerBase
    {
        /// <summary>
        /// 成功
        /// </summary>
        /// <returns></returns>
        protected IActionResult Success()
        {
            return Ok(ResponseResult<string>.Success("操作成功"));
        }

        /// <summary>
        /// 成功
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="result"></param>
        /// <returns></returns>
        protected IActionResult Success<T>(T result)
        {
            return Ok(ResponseResult<T>.Success(result));
        }

        protected IActionResult Fail(string msg)
        {
            Response.StatusCode = 500;
            return Ok(ResponseResult<string>.Fail(msg));
        }

        /// <summary>
        /// jwt中获取用户
        /// </summary>
        /// <returns></returns>
        protected string LoginUserInfo()
        {
            var jwt = Request.Headers.Authorization.ToString();
            var handler = new JwtSecurityTokenHandler();
            var uid = handler.ReadJwtToken(jwt).Claims.FirstOrDefault(r=>r.Type=="UID").Value;
            return uid;
        }
    }
}
